<?php

class Logs{
	var $FilePath = "/log/";
	var $FileName = "test_app.log";
	
	static private $mInstance = null;
	
	static public function &Instance()
	{
		if ( null == self::$mInstance )
		{ 
			$class = __CLASS__;
			self::$mInstance = new $class;
		}
		return self::$mInstance;
	}
	

	function __construct(){
		$this->FilePath = WWW_ROOT.$this->FilePath;
		$path=$this->createPath($this->FilePath,$this->FileName);
		
		if(!$this->isExist($path)){
			if(!$this->createDir($this->FilePath)){
				die("this path can not be  created22!!".WWW_ROOT);
			}
			
			if(!$this->createLogFile($path)){
				die("this file can not be created1221".$path);
			}
		}
	}
	

	static public function setLog($log){
		//echo "$log";
		/*exit;*/
		self::Instance();
		$log = date("Y-m-d H:i:s")."==>  ".$log;
		$path= self::$mInstance->createPath(self::$mInstance->FilePath,self::$mInstance->FileName);
		$handle=fopen($path,"a+");
		if(!fwrite($handle,$log."\n")){
			die("error||".self::$mInstance->FilePath.self::$mInstance->FileName);
		}
		
		fclose($handle);
	}
	

    function isExist($path){
		return file_exists($path);
	}
	

	function createDir($dir){
		return is_dir($dir) or ($this->createDir(dirname($dir)) and mkdir($dir, 0777));
	}
	

	function createLogFile($path){
		$handle=fopen($path,"w");
		fclose($handle);
		return $this->isExist($path);
    }
	
	function createPath($dir,$filename){
		return $dir."/".$filename;
	}
}
?>
